<?php

/**
 * This is the model class for table "{{person_task}}".
 *
 * The followings are the available columns in table '{{person_task}}':
 * @property integer $ptid
 * @property integer $pdid
 * @property integer $tid
 * @property integer $total_complete
 * @property integer $today_extra
 * @property integer $time_spent
 * @property integer $transactor
 * @property integer $status
 * @property integer $dateline
 * @property integer $updated
 */
class PersonTask extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @return PersonTask the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{person_task}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('pdid, tid, total_complete, today_extra, time_spent, transactor, status, dateline, updated', 'numerical', 'integerOnly' => true),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('ptid, pdid, tid, total_complete, today_extra, time_spent, transactor, status, dateline, updated', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'personDate' => array(self::BELONGS_TO, 'PersonDate', 'pdid'),
            'task' => array(self::BELONGS_TO, 'Task', 'tid'),
            'user' => array(self::BELONGS_TO, 'User', 'transactor'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'ptid' => 'Ptid',
            'pdid' => 'Pdid',
            'tid' => 'Tid',
            'total_complete' => 'Total Complete',
            'today_extra' => 'Today Extra',
            'time_spent' => 'Time Spent',
            'transactor' => 'Transactor',
            'status' => 'Status',
            'dateline' => 'Dateline',
            'updated' => 'Updated',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('ptid', $this->ptid);
        $criteria->compare('pdid', $this->pdid);
        $criteria->compare('tid', $this->tid);
        $criteria->compare('total_complete', $this->total_complete);
        $criteria->compare('today_extra', $this->today_extra);
        $criteria->compare('time_spent', $this->time_spent);
        $criteria->compare('transactor', $this->transactor);
        $criteria->compare('status', $this->status);
        $criteria->compare('dateline', $this->dateline);
        $criteria->compare('updated', $this->updated);

        //只看自己的personTask
        $criteria->with = 'personDate';
        $criteria->addCondition("personDate.uid=" . Yii::app()->user->id); //只能看见属于自己的personDate


        return new CActiveDataProvider(get_class($this), array(
            'criteria' => $criteria,
        ));
    }

    public function belongSearch($pdid) {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('ptid', $this->ptid);
        $criteria->compare('pdid', $this->pdid);
        $criteria->compare('tid', $this->tid);
        $criteria->compare('total_complete', $this->total_complete);
        $criteria->compare('today_extra', $this->today_extra);
        $criteria->compare('time_spent', $this->time_spent);
        $criteria->compare('transactor', $this->transactor);
        $criteria->compare('t.status', $this->status);
        $criteria->compare('dateline', $this->dateline);
        $criteria->compare('updated', $this->updated);
        $criteria->with = 'task';

        $personTasks = PersonTask::model()->findAll('pdid=:pdID', array('pdID' => (int) $pdid));

        $idArr = array();
        foreach ($personTasks as $personTask) {
            $idArr[] = $personTask['ptid'];
        }
        $criteria->addInCondition('ptid', $idArr); //与上面正好相法，是NOT IN

        return new CActiveDataProvider(get_class($this), array(
            'criteria' => $criteria,
        ));
    }

    protected function beforeSave() {
        if (parent::beforeSave()) {
            if ($this->isNewRecord) {
                $this->dateline = $this->updated = time();
            } else {
                $this->updated = time();
            }
            return true;
        } else {
            return false;
        }
    }

    //保存完成记录，从0更新成1时，需要更新管理的Task的完成度
    protected function afterSave() {
        parent::afterSave();
        if (is_numeric((int) $this->status) && $this->status > 0) {
            Task::model()->updateComplete($this->tid, (int) $this->today_extra);
        }
    }

    //
    public function findJobTask($status, $jobId = 0) {
        $condition = 't.status=' . $status;
        if ($jobId) {
            $condition .= ' and pdid=' . $jobId;
        }
        return $this->findAll(array(
            'condition' => $condition,
        ));
    }

}